<?php

namespace IDock\Insurance\QHIC\Request;

use IDock\Insurance\QHIC\QHICEnums;
use IDock\Insurance\QHIC\Request\AllQHICRequest;
use IDock\Insurance\QHIC\Response\UnderwritingResponse;
use IDock\Visitor\Premium\PremiumInquiryVisitor;
use IDock\Visitor\SpecialClausesVisitor;

class UnderwritingRequest extends AllQHICRequest
{
    public $requestType = 'VHL0007';

    protected $response = UnderwritingResponse::class;

    protected $urlTip = '申请核保';

    /*** @var  PremiumInquiryVisitor $visitor */
    protected $visitor;

    public function generate(): array
    {
        $arr = [
            'Base' =>[
                'CAppNo' => $this->visitor->getUnderwritingVisitor()->ciInquiryNo,
                'SY_CAppNo' => $this->visitor->getUnderwritingVisitor()->biInquiryNo,
                'UnderContent' => $this->visitor->getExtInfoVisitor()->underwriteMsg,
            ],
            'Insured' => $this->getInsured(),
            'Applicant' => $this->getApplicant(),
            'FixSpecList' => $this->special(),
        ];
        return $arr;
    }

    private function getApplicant()
    {
        $model = $this->visitor->getApplicantVisitor();
        $arr = [
            'CAppNme' => $model->name,
            'CCertfCls' => QHICEnums::$certificateTypeCode[$model->certificateTypeCode] ?? '',
            'CCertfCde' => $model->certificateNo,
            'CClntMrk' => $model->typeCode == 'R1' ? '1' : '3',
            'CClntAddr' => $model->address,
            'CAppCate' => '1',//投保人类别
            'CMobile' => $model->mobile,
            'CTaxpayerTyp' => '2',//纳税资质 非纳税人
            'CInvoiceTyp' => $model->typeCode == 'R1' ? 'A' : 'B',
            'CImediumTyp' => '',
            'CEmail' => $model->email,
            'TCertfEndDate' => date('Ymd',strtotime($model->certificateEndDate)),
            'CCertfMrk' => $model->certificateEndDate == '2099-12-31' ? '1' : '0',
            'CNationality' => '156',//国籍 China
            'COccupCde' => '',//职业
            'CResvTxt11' => '',//职业名称
            'CLegalNme' => '',//法定代表人
        ];
        if($arr['CInvoiceTyp'] == 'A'){
            $arr['CImediumTyp'] = '2';
        }
        if($model->typeCode == 'R1'){
            $arr['COccupCde'] = '40000';
            $arr['CResvTxt11'] = '社会生产服务和生活服务人员';
        }else{
            $arr['CLegalNme'] = $this->visitor->getOwnerVisitor()->name;
        }
        return $arr;
    }

    private function getInsured()
    {
        $model = $this->visitor->getInsuredVisitor();
        return [
            'CInsuredNme' => $model->name,
            'CCertfCls' => QHICEnums::$certificateTypeCode[$model->certificateTypeCode] ?? '',
            'CCertfCde' => $model->certificateNo,
            'CClntMrk' => $model->typeCode == 'R1' ? '1' : '3',
            'CResvTxt20' => '1',//被保险人类别
            'CMobile' => $model->mobile,
            'CClntAddr' => $model->address,
            'CEmail' => $model->email,
            'TCertfEndDate' => date('Ymd',strtotime($model->certificateEndDate)),
            'CIsInsideCar' => '0',
        ];
    }

    private function special()
    {
        $list = [];
        foreach ($this->visitor->getUnderwritingVisitor()->specialClausesList as $value){
            /**@var SpecialClausesVisitor $value * */
            $list[] = [
                'CSpecNo' => $value->clauseCode,
                'CSpecContent ' => $value->clauseContent
            ];
        }
        return $list;
    }


}
